﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Dashboard
{
    public class ClusterNode : Canvas
    {
        public event OnClusterNodeSelected ClusterNodeSelected;

        #region data
        public int ClusterID { get; set; }
        private bool _Collapsed;
        public bool Collapsed
        {
            get
            {
                return _Collapsed;
            }
            set
            {
                if(_Collapsed !=value)
                {
                    this._Collapsed = value;
                    this.Draw();
                }
            }
        }
        public double Diameter { get; set; }
        public string Keywords { get; set; }
        public Dictionary<int,Dictionary<int,double>> DocDocDistances { get; set; }
        #endregion

        #region ui 
        private Color bgColor = Color.FromArgb(125, 234, 234, 234);
        private Color borderColor = Color.FromArgb(240, 120, 120, 120);
        private Ellipse collapsedCircle;
        private TextBlock clusterLabel;
        private TextBlock tooltipControl;
        private List<DocumentNode> docNodes;
        #endregion

        #region ctor
        public ClusterNode()
        {
            this.ClusterID = 0;
            this.Collapsed = true;
            this.Diameter = 10;
            this.Keywords = string.Empty;
            this.DocDocDistances=new Dictionary<int, Dictionary<int, double>>();
            this.docNodes=new List<DocumentNode>();
            
            this.collapsedCircle=new Ellipse();
            this.collapsedCircle.SetValue(WidthProperty, this.Diameter);
            this.collapsedCircle.SetValue(HeightProperty, this.Diameter);
            this.collapsedCircle.Fill = new SolidColorBrush(bgColor);
            this.collapsedCircle.Stroke = new SolidColorBrush(borderColor);
            this.collapsedCircle.StrokeDashArray=new DoubleCollection();
            this.collapsedCircle.StrokeDashArray.Add(3.0);

            string label = "cluster " + ClusterID;
            this.clusterLabel=new TextBlock();
            this.clusterLabel.Text = label;
            string tooltipText = string.Format(
                "Cluster #{0}, docs: {1}, diameter: {2}",
                this.ClusterID, this.docNodes.Count, this.Diameter);
            this.tooltipControl = new TextBlock();
            this.tooltipControl.Text = tooltipText;
            ToolTipService.SetToolTip(this.collapsedCircle, tooltipControl);

            this.collapsedCircle.MouseLeftButtonDown += new MouseButtonEventHandler(collapsedCircle_MouseLeftButtonDown);
        }

        void collapsedCircle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Collapsed = false;
            if (this.ClusterNodeSelected != null)
                this.ClusterNodeSelected(this.ClusterID);
        }
        #endregion

        #region binding
        public void BindData(
            int clusterID, double diameter, string keywords,
            List<int> includedDocIDs,
            Dictionary<int,string> docTitles,
            Dictionary<int,Dictionary<int,double>> docDocDistances)
        {
            this.ClusterID = clusterID;
            this.Diameter = diameter;
            this.Keywords = keywords;
            this.DocDocDistances = docDocDistances;

            this.docNodes=new List<DocumentNode>();
            foreach(int docID in includedDocIDs)
            {
                DocumentNode docNode=new DocumentNode();
                docNode.BindData(docID, docTitles[docID]);
                this.docNodes.Add(docNode);
            }
            this.Draw();
        }
        #endregion

        #region draw
        private void Draw()
        {
            if(this.Collapsed)
            {
                this.collapsedCircle.Visibility = Visibility.Visible;
                foreach (DocumentNode docNode in this.docNodes)
                    docNode.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.collapsedCircle.Visibility = Visibility.Collapsed;
                foreach (DocumentNode docNode in this.docNodes)
                    docNode.Visibility = Visibility.Collapsed;
            }
        }
        #endregion
    }
}
